package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmCluepoolLinkmanService;
import com.mdd.admin.validate.ClientCrmCluepoolLinkmanCreateValidate;
import com.mdd.admin.validate.ClientCrmCluepoolLinkmanUpdateValidate;
import com.mdd.admin.validate.ClientCrmCluepoolLinkmanSearchValidate;
import com.mdd.admin.vo.ClientCrmCluepoolLinkmanListedVo;
import com.mdd.admin.vo.ClientCrmCluepoolLinkmanDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmCluepoolLinkman;
import com.mdd.common.mapper.ClientCrmCluepoolLinkmanMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 线索联系人实现类
 * @author 洁洁
 */
@Service
public class ClientCrmCluepoolLinkmanServiceImpl implements IClientCrmCluepoolLinkmanService {
        
    @Resource
    ClientCrmCluepoolLinkmanMapper clientCrmCluepoolLinkmanMapper;

    /**
     * 线索联系人列表
     *
     * @author 洁洁
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmCluepoolLinkmanListedVo>
     */
    @Override
    public PageResult<ClientCrmCluepoolLinkmanListedVo> list(PageValidate pageValidate, ClientCrmCluepoolLinkmanSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmCluepoolLinkman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        if (searchValidate.getThreadId() != null) {
            queryWrapper.eq("thread_id", searchValidate.getThreadId());
        }
        if (searchValidate.getCustomerMianId() != null) {
            queryWrapper.eq("customer_mian_id",searchValidate.getCustomerMianId());
        }
        queryWrapper.orderByDesc(Arrays.asList("sort", "create_time"));

        clientCrmCluepoolLinkmanMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
                "=:systemType@system_type:int",
        });

        IPage<ClientCrmCluepoolLinkman> iPage = clientCrmCluepoolLinkmanMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmCluepoolLinkmanListedVo> list = new LinkedList<>();
        for(ClientCrmCluepoolLinkman item : iPage.getRecords()) {
            ClientCrmCluepoolLinkmanListedVo vo = new ClientCrmCluepoolLinkmanListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }
    /**
     * 线索联系人列表
     *
     * @author 洁洁
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmCluepoolLinkmanListedVo>
     */
    @Override
    public List<ClientCrmCluepoolLinkmanListedVo> all(ClientCrmCluepoolLinkmanSearchValidate searchValidate) {
        QueryWrapper<ClientCrmCluepoolLinkman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        if (searchValidate.getThreadId() != null) {
            queryWrapper.eq("thread_id", searchValidate.getThreadId());
        }
        if (searchValidate.getCustomerMianId() != null) {
            queryWrapper.eq("customer_mian_id",searchValidate.getCustomerMianId());
        }
        if (searchValidate.getCaseType() != null) {
            queryWrapper.like("case_type",searchValidate.getCaseType());
        }
        queryWrapper.orderByDesc(Arrays.asList("sort", "create_time"));

        clientCrmCluepoolLinkmanMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "like:name:str",
                "=:systemType@system_type:int",
                "=:userId@user_id:int",
        });

        List<ClientCrmCluepoolLinkman> iPage = clientCrmCluepoolLinkmanMapper.selectList(queryWrapper);

        List<ClientCrmCluepoolLinkmanListedVo> list = new LinkedList<>();
        for(ClientCrmCluepoolLinkman item : iPage) {
            ClientCrmCluepoolLinkmanListedVo vo = new ClientCrmCluepoolLinkmanListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return list;
    }
    /**
     * 线索联系人详情
     *
     * @author 洁洁
     * @param id 主键参数
     * @return ClientCrmCluepoolLinkman
     */
    @Override
    public ClientCrmCluepoolLinkmanDetailVo detail(Integer id) {
        ClientCrmCluepoolLinkman model = clientCrmCluepoolLinkmanMapper.selectOne(
                new QueryWrapper<ClientCrmCluepoolLinkman>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmCluepoolLinkmanDetailVo vo = new ClientCrmCluepoolLinkmanDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 线索联系人新增
     *
     * @author 洁洁
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmCluepoolLinkmanCreateValidate createValidate) {
        ClientCrmCluepoolLinkman model = new ClientCrmCluepoolLinkman();
        model.setName(createValidate.getName());
        model.setWorkEmail(createValidate.getWorkEmail());
        model.setCustomerMianId(createValidate.getCustomerMianId());
        model.setThreadId(createValidate.getThreadId());
        model.setPosition(createValidate.getPosition());
        model.setEmail(createValidate.getEmail());
        model.setIsOnJob(createValidate.getIsOnJob());
        model.setCallName(createValidate.getCallName());
        model.setSex(createValidate.getSex());
        model.setType(createValidate.getType());
        model.setSystemType(createValidate.getSystemType());
        model.setCaseType(createValidate.getCaseType().toString());
        model.setNumber(createValidate.getNumber());
        model.setPhone(createValidate.getPhone());
        model.setFax(createValidate.getFax());
        model.setQq(createValidate.getQq());
        model.setWechat(createValidate.getWechat());
        model.setSort(createValidate.getSort());
        model.setWorkAddress(createValidate.getWorkAddress());
        model.setOldAddress(createValidate.getOldAddress());
        model.setHomeAddress(createValidate.getHomeAddress());
        model.setPostcode(createValidate.getPostcode());
        model.setHobby(createValidate.getHobby());
        model.setRemark(createValidate.getRemark());
        model.setUserId(LikeAdminThreadLocal.getAdminId());
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setIsDelete(0);
        clientCrmCluepoolLinkmanMapper.insert(model);
    }

    /**
     * 线索联系人编辑
     *
     * @author 洁洁
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmCluepoolLinkmanUpdateValidate updateValidate) {
        ClientCrmCluepoolLinkman model = clientCrmCluepoolLinkmanMapper.selectOne(
                new QueryWrapper<ClientCrmCluepoolLinkman>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setWorkEmail(updateValidate.getWorkEmail());
        model.setPosition(updateValidate.getPosition());
        model.setEmail(updateValidate.getEmail());
        model.setIsOnJob(updateValidate.getIsOnJob());
        model.setCallName(updateValidate.getCallName());
        model.setSex(updateValidate.getSex());
        model.setType(updateValidate.getType());
        model.setSystemType(updateValidate.getSystemType());
        model.setCaseType(updateValidate.getCaseType().toString());
        model.setNumber(updateValidate.getNumber());
        model.setPhone(updateValidate.getPhone());
        model.setFax(updateValidate.getFax());
        model.setQq(updateValidate.getQq());
        model.setWechat(updateValidate.getWechat());
        model.setSort(updateValidate.getSort());
        model.setWorkAddress(updateValidate.getWorkAddress());
        model.setOldAddress(updateValidate.getOldAddress());
        model.setHomeAddress(updateValidate.getHomeAddress());
        model.setPostcode(updateValidate.getPostcode());
        model.setHobby(updateValidate.getHobby());
        model.setRemark(updateValidate.getRemark());
        clientCrmCluepoolLinkmanMapper.updateById(model);
    }

    /**
     * 线索联系人删除
     *
     * @author 洁洁
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmCluepoolLinkman model = clientCrmCluepoolLinkmanMapper.selectOne(
                new QueryWrapper<ClientCrmCluepoolLinkman>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmCluepoolLinkmanMapper.updateById(model);
    }

}
